<?php

class ZalogowaniController extends Zend_Controller_Action {

    static $daneForm;
    static $idedyt;

    public function init() {
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->user = new Application_Model_Auth_Uzytkownicy();
        $this->view->dane = $this->view->user->fetchAll();
        $this->view->films = new Application_Model_Auth_Filmy();
        $_SESSION['param'] = 'zwykly';

 if (Zend_Auth::getInstance()->hasIdentity()){
        $this->view->login = Zend_Auth::getInstance()->getStorage()->read()->login;
        $this->view->iduser = Zend_Auth::getInstance()->getStorage()->read()->iduser;
        $this->view->plec = Zend_Auth::getInstance()->getStorage()->read()->gender;
        $this->view->email = Zend_Auth::getInstance()->getStorage()->read()->email;
        $idacc = Zend_Auth::getInstance()->getStorage()->read()->idacctype;
        $db = Zend_Db_Table::getDefaultAdapter();
        $query = $db->select()
                ->from('acctype', 'type')
                ->where('idacctype = ?', $idacc);
        $result = $db->fetchRow($query);

        $role = $result['type'];
        $this->view->rola = $role;
        /*if ($this->view->rola == "admin") {
            $this->_helper->layout->setLayout('layoutadmin');
        } else if ($this->view->rola == "zwykly") {
            $this->_helper->layout->setLayout('layout');
        } else {
            $this->_helper->layout->setLayout('layout');
        }*/


        $this->form_edyt_user = new Application_Form_EdytujkontoForm();
        $this->view->form_edyt_user = $this->form_edyt_user;
        $this->form_admin_edyt_user = new Application_Form_AdminedytujkontoForm();
        $this->view->form_admin_edyt_user = $this->form_admin_edyt_user;
        $this->form_dodaj_user = new Application_Form_DodajkontoForm();
        $this->view->form_dodaj_user = $this->form_dodaj_user;
        $this->form_dodaj_film = new Application_Form_DodajfilmForm();
        $this->view->form_dodaj_film = $this->form_dodaj_film;
    }
    $this->form = new Application_Form_LoginForm();
    $this->form_szukaj = new Application_Form_SzukajForm();
        $this->view->form_szukaj=$this->form_szukaj;
}
    public function indexAction() {
        
    }

    public function zwyklyAction() {
        
    }

    public function adminAction() {
        
    }

    public function filmysdAction() {
        $this->view->title = "Baza filmów";
        $wyswietlaniesd = new Application_Model_Movies_Filmy();
        $this->view->filmysd=$wyswietlaniesd->filmysd();
        
        $_SESSION['akcja'] = 'ogladajsd';
        $_SESSION['movietype'] = '1';
        $_SESSION['param'] = 'filmy';
        
        if ($this->view->rola == "admin") {
            $this->_helper->layout->setLayout('layoutfilmyadmin');
        } else if ($this->view->rola == "zwykly" || $this->view->rola == "premium") {
            $this->_helper->layout->setLayout('layoutfilmyuzyt');
        } else {
            $this->_helper->layout->setLayout('layoutfilmy');
        }
         
    }
    public function filmyhdAction() {
        $this->view->title = "Baza filmów";
        $wyswietlaniehd = new Application_Model_Movies_Filmy();
        $this->view->filmyhd=$wyswietlaniehd->filmyhd();
        
         $_SESSION['param'] = 'filmy';
         $_SESSION['akcja'] = 'ogladajhd';
        $_SESSION['movietype'] = '2';
        
        if ($this->view->rola == "admin") {
            $this->_helper->layout->setLayout('layoutfilmyadmin');
        } else if ($this->view->rola == "zwykly" || $this->view->rola == "premium") {
            $this->_helper->layout->setLayout('layoutfilmyuzyt');
        } else {
            $this->_helper->layout->setLayout('layoutfilmy');
        };
        //$idedyt = $this->_request->getParam('idacctype');
        //$this->_redirect('/zalogowani/poedycjifilmu?idedyt=' . $this->view->idacctype);
        //if(Zend_Auth::getInstance()->getStorage()->read()->idacctype){
        //$acctype = Zend_Auth::getInstance()->getStorage()->read()->idacctype;}
        //Zend_Debug::dump($acctype);
        //$this->view->idacctype = $acctype;
    }
    public function filmyhdfreeAction() {
        $this->view->title = "Baza filmów";
        $wyswietlaniehdfree = new Application_Model_Movies_Filmy();
        $this->view->filmyhdfree=$wyswietlaniehdfree->filmyhdfree();
        
         $_SESSION['param'] = 'zwykly';
         $_SESSION['akcja'] = 'ogladajhdfree';
        $_SESSION['movietype'] = '2';
        
        if ($this->view->rola == "admin") {
            $this->_helper->layout->setLayout('layoutfilmyadmin');
        } else if ($this->view->rola == "zwykly" || $this->view->rola == "premium") {
            $this->_helper->layout->setLayout('layoutfilmyuzyt');
        } else {
            $this->_helper->layout->setLayout('layoutfilmy');
        };
        //$idedyt = $this->_request->getParam('idacctype');
        //$this->_redirect('/zalogowani/poedycjifilmu?idedyt=' . $this->view->idacctype);
        //if(Zend_Auth::getInstance()->getStorage()->read()->idacctype){
        //$acctype = Zend_Auth::getInstance()->getStorage()->read()->idacctype;}
        //Zend_Debug::dump($acctype);
        //$this->view->idacctype = $acctype;
    }
    public function serialeAction() {
        $this->view->title = "Baza seriali";
        $wyswietlanieser = new Application_Model_Movies_Filmy();
        $this->view->seriale=$wyswietlanieser->seriale();
        
         $_SESSION['param'] = 'filmy';
         $_SESSION['akcja'] = 'ogladajserial';
         $_SESSION['movietype'] = '3';
        
        if ($this->view->rola == "admin") {
            $this->_helper->layout->setLayout('layoutfilmyadmin');
        } else if ($this->view->rola == "zwykly" || $this->view->rola == "premium") {
            $this->_helper->layout->setLayout('layoutfilmyuzyt');
        } else {
            $this->_helper->layout->setLayout('layoutfilmy');
        }
    }
    public function juniorAction() {
        $this->view->title = "Baza filmów dla dzieci";
        $wyswietlaniejun = new Application_Model_Movies_Filmy();
        $this->view->seriale=$wyswietlaniejun->junior();
        
         $_SESSION['param'] = 'filmy';
         $_SESSION['akcja'] = 'ogladajjunior';
         $_SESSION['movietype'] = '4';
        
        if ($this->view->rola == "admin") {
            $this->_helper->layout->setLayout('layoutfilmyadmin');
        } else if ($this->view->rola == "zwykly" || $this->view->rola == "premium") {
            $this->_helper->layout->setLayout('layoutfilmyuzyt');
        } else {
            $this->_helper->layout->setLayout('layoutfilmy');
        }
    }
    public function ogladajhdAction() {
        $this->view->title = "Informacje o filmie";
        $ogladaniehd = new Application_Model_Watch_Ogladaj();
        $film = $this->getRequest()->getParam('nazwa');
        $this->view->ogladajhd=$ogladaniehd->ogladajhd($film);
        
        $dane = $this->view->user->getUser($this->view->iduser);
        $this->view->iduser = $dane['iduser'];
        $ulub2=new Application_Model_Auth_Ulubione;
        
        $changes = new Zend_Form_Element_Submit('ulubiony');
        $changes->setLabel('Dodaj do ulubionych');
        $this->view->button=$changes;
        
        if ($this->_request->isPost()) {
            $ulub2->addUlubione($_POST['user'],$_POST['movie']);
            if($_SERVER['PHP_SELF']=='/aplikacja/public/index.php'){
            header('Location: ' . $_SERVER['PHP_SELF'].'/zalogowani/ogladajhd/iduser/'.$this->view->iduser.'/nazwa/'.$film);    
                }else{
            header('Location: ' . $_SERVER['PHP_SELF'].'/'); 
                }  
        }
    }
    public function ogladajsdAction() {
        $this->view->title = "Informacje o filmie";
        $ogladaniesd = new Application_Model_Watch_Ogladaj();
        $film = $this->getRequest()->getParam('nazwa');
        $this->view->ogladajsd=$ogladaniesd->ogladajsd($film);
        
        $dane = $this->view->user->getUser($this->view->iduser);
        $this->view->iduser = $dane['iduser'];
        $ulub2=new Application_Model_Auth_Ulubione;
        
        $changes = new Zend_Form_Element_Submit('ulubiony');
        $changes->setLabel('Dodaj do ulubionych');
        $this->view->button=$changes;
        
        if ($this->_request->isPost()) {
            $ulub2->addUlubione($_POST['user'],$_POST['movie']);
            if($_SERVER['PHP_SELF']=='/aplikacja/public/index.php'){
            header('Location: ' . $_SERVER['PHP_SELF'].'/zalogowani/ogladajsd/iduser/'.$this->view->iduser.'/nazwa/'.$film);    
                }else{
            header('Location: ' . $_SERVER['PHP_SELF'].'/'); 
                }   
        }
    }
    public function ogladajserialAction() {
        $this->view->title = "Informacje o serialu";
        $ogladanieser = new Application_Model_Watch_Ogladaj();
        $film = $this->getRequest()->getParam('nazwa');
        $this->view->ogladajserial=$ogladanieser->ogladajserial($film);
        
        $dane = $this->view->user->getUser($this->view->iduser);
        $this->view->iduser = $dane['iduser'];
        $ulub2=new Application_Model_Auth_Ulubione;
        
        $changes = new Zend_Form_Element_Submit('ulubiony');
        $changes->setLabel('Dodaj do ulubionych');
        $this->view->button=$changes;
        
        if ($this->_request->isPost()) {
            $ulub2->addUlubione($_POST['user'],$_POST['movie']);
            if($_SERVER['PHP_SELF']=='/aplikacja/public/index.php'){
            header('Location: ' . $_SERVER['PHP_SELF'].'/zalogowani/ogladajserial/iduser/'.$this->view->iduser.'/nazwa/'.$film);    
                }else{
            header('Location: ' . $_SERVER['PHP_SELF'].'/'); 
                }  
        }
    }
    public function ogladajjuniorAction() {
        $this->view->title = "Informacje filmie";
        $ogladaniejun = new Application_Model_Watch_Ogladaj();
        $film = $this->getRequest()->getParam('nazwa');
        $this->view->ogladajjunior=$ogladaniejun->ogladajjunior($film);
        
          $dane = $this->view->user->getUser($this->view->iduser);
        $this->view->iduser = $dane['iduser'];
        $ulub2=new Application_Model_Auth_Ulubione;
        
        $changes = new Zend_Form_Element_Submit('ulubiony');
        $changes->setLabel('Dodaj do ulubionych');
        $this->view->button=$changes;
        
        if ($this->_request->isPost()) {
            $ulub2->addUlubione($_POST['user'],$_POST['movie']);
            if($_SERVER['PHP_SELF']=='/aplikacja/public/index.php'){
            header('Location: ' . $_SERVER['PHP_SELF'].'/zalogowani/ogladajjunior/iduser/'.$this->view->iduser.'/nazwa/'.$film);    
                }else{
            header('Location: ' . $_SERVER['PHP_SELF'].'/'); 
                }  
        }
    }
    
    function profilkontaAction() {
        $this->view->title = "Twoje dane";

        $dane = $this->view->user->getUser($this->view->iduser);
        $this->view->iduser = $dane['iduser'];
        $this->view->plec = $dane['gender'];
        $this->view->email = $dane['email'];
    }

    public function edytujkontoAction() {

        if ($this->_request->isPost()) {
            self::$daneForm = $this->_request->getPost();
            if ($this->form_edyt_user->isValid(self::$daneForm)) {
                $this->oldpass = $this->form_edyt_user->getValue('oldpassword');
                $this->newpass = $this->form_edyt_user->getValue('newpassword');
                $this->reppass = $this->form_edyt_user->getValue('repeatpassword');
                $this->plec = $this->form_edyt_user->getValue('gender');
                $this->email = $this->form_edyt_user->getValue('email');

                $db = Zend_Db_Table::getDefaultAdapter();

                //sprawdzenie czy istnieje juz taki email w bazie
                $queryemail = $db->select()
                        ->from('user', 'email')
                        ->where('email = ?', $this->email);

                $resultemail = $db->fetchRow($queryemail);


                //zwrocenie id uzytkownika o podanym emailu z bazy jesli istnieje

                $queryid = $db->select()
                        ->from('user', 'iduser')
                        ->where('email = ?', $this->email);

                $resultid = $db->fetchRow($queryid);


                //zwrocenie hasla uzytkownika o podanym id z bazy      
                $queryhaslo = $db->select()
                        ->from('user', 'password')
                        ->where('iduser = ?', $this->view->iduser);

                $resulthaslo = $db->fetchRow($queryhaslo);

                $starehaslo = sha1(self::$daneForm['oldpassword']);

                //sprawdzenie poprawnosci wpisanych danych
                if ($resultemail && ($resultid['iduser'] != $this->view->iduser)) {
                    echo "<p><h3>Użytkownik o podanym adresie email już istnieje, podaj inny email!</h3></p>";
                    //echo $this->form_edyt_user;
                    return;
                }

                if ($resulthaslo['password'] != $starehaslo) {
                    echo "<p><h3>Stare haslo: podane haslo nie jest poprawne!</h3></p>";
                    //echo $this->form_edyt_user;
                    return;
                }

                if ($this->newpass != $this->reppass) {
                    echo "<p><h3>Nowe haslo: podane hasla nie są identyczne!</h3></p>";
                    //echo $this->form_edyt_user;
                    return;
                }




                $haslo = sha1(self::$daneForm['newpassword']);
                $user = new Application_Model_Auth_Uzytkownicy();
                $user->editUser($this->view->iduser, $this->email, $haslo, $this->plec);


                $this->view->haslo = $this->newpass;
                $this->view->plec = $this->plec;
                $this->view->email = $this->email;

                echo $this->view->haslo . " " . $this->view->plec . " " . $this->view->email . " "
                . $this->view->id . " " . $this->view->login;



                $this->_helper->redirector('poedycjikonta');
            } else {
                $this->form_edyt_user->populate(self::$daneForm);
            }
        } else {
            $id = $this->_getParam('iduser', 0);
            if ($id > 0) {
                $user = new Application_Model_Auth_Uzytkownicy();
                //$student = new BazaZF_Student();
                $this->form_edyt_user->populate($user->getUser($id));
            }
        }
    }

    public function poedycjikontaAction() {
        $this->view->title = "Twoje dane zostały zmienione:";
        $dane = $this->view->user->getUser($this->view->iduser);
        $this->view->iduser = $dane['iduser'];
        $this->view->plec = $dane['gender'];
        $this->view->email = $dane['email'];
    }
    
    public function listaulubionychAction(){
        $this->view->title = "Lista Ulubionych";
        $dane = $this->view->user->getUser($this->view->iduser);
        $this->view->iduser = $dane['iduser'];
        
        $ulub = new Application_Model_Favorites_Ulubione();
        $ulub2 = new Application_Model_Auth_Ulubione();
        $this->view->ulub = $ulub->listaUlubione($this->view->iduser);
        
        $changes = new Zend_Form_Element_Submit('usun');
        $changes->setLabel('Usuń')
                ->removeDecorator('DtDdWrapper');
        $this->view->button=$changes;
    
        if ($this->_request->isPost()) {
            $ulub2->deleteUlubione($_POST['id']);
            if($_SERVER['PHP_SELF']=='/aplikacja/public/index.php'){
            header('Location: ' . $_SERVER['PHP_SELF'].'/zalogowani/listaulubionych/iduser/'.$this->view->iduser);    
                }else{
            header('Location: ' . $_SERVER['PHP_SELF'].'/'); 
                }
        }
    }
    
    public function adminlistaulubionychAction(){
        $this->view->iduser=$this->_request->getParam('iduser');
        $dane = $this->view->user->getUser($this->view->iduser);
       // $this->view->iduser = $dane['iduser'];
        $this->view->title = "Lista Ulubionych: ".$dane['login'];
        
        $ulub = new Application_Model_Favorites_Ulubione();
        $ulub2 = new Application_Model_Auth_Ulubione();
        $this->view->ulub = $ulub->listaUlubione($this->view->iduser);
        
        $changes = new Zend_Form_Element_Submit('usun');
        $changes->setLabel('Usuń')
                ->removeDecorator('DtDdWrapper');
        $this->view->button=$changes;
    
        if ($this->_request->isPost()) {
            $ulub2->deleteUlubione($_POST['id']);
            
            if($_SERVER['PHP_SELF']=='/aplikacja/public/index.php'){
            header('Location: ' . $_SERVER['PHP_SELF'].'/zalogowani/adminlistaulubionych/iduser/'.$this->view->iduser);    
                }else{
            header('Location: ' . $_SERVER['PHP_SELF'].'/'); 
                } 
        }
    }

    public function adminuzytkownicyAction() {
        $this->view->title = "Lista Użytkowników";
        $this->view->user = $this->view->user->fetchAll();
    }

    public function adminedytujkontoAction() {
        $this->view->title = "Edytuj dane";


        if ($this->_request->isPost()) {
            self::$daneForm = $this->_request->getPost();
            if ($this->form_admin_edyt_user->isValid(self::$daneForm)) {
                $this->login = $this->form_admin_edyt_user->getValue('login');
                $this->email = $this->form_admin_edyt_user->getValue('email');
                $this->plec = $this->form_admin_edyt_user->getValue('gender');
                $this->rola = $this->form_admin_edyt_user->getValue('idacctype');

                $db = Zend_Db_Table::getDefaultAdapter();

                //sprawdzenie czy istnieje juz taki email w bazie
                $queryemail = $db->select()
                        ->from('user', 'email')
                        ->where('email = ?', $this->email);

                $resultemail = $db->fetchRow($queryemail);


                //zwrocenie id uzytkownika o podanym emailu z bazy jesli istnieje
                $queryid = $db->select()
                        ->from('user', 'iduser')
                        ->where('email = ?', $this->email);

                $resultid = $db->fetchRow($queryid);


                //sprawdzenie poprawnosci wpisanych danych
                if ($resultemail && ($resultid['iduser'] != $this->_request->getParam('iduser'))) {
                    echo "<p><h3>Użytkownik o podanym adresie email już istnieje, podaj inny email!</h3></p>";
                    //echo $this->form_edyt_user;
                    return;
                }


                $user = new Application_Model_Auth_Uzytkownicy();
                $user->admineditUser($this->_request->getParam('iduser'), $this->login, $this->email, $this->plec, $this->rola);

                $this->view->login = $this->login;
                $this->view->rola = $this->rola;
                $this->view->plec = $this->plec;
                $this->view->email = $this->email;


                $idedyt = $this->_request->getParam('iduser');

                //echo $this->view->haslo . " " . $this->view->plec . " " . $this->view->email . " "
                //. $this->view->id . " " . $this->view->login;
                //$this->_helper->redirector('adminpoedycjikonta','zalogowani', array('idedyt' => $idedyt));
                $this->_redirect('/zalogowani/adminpoedycjikonta?idedyt=' . $idedyt);
            } else {
                $this->form_admin_edyt_user->populate(self::$daneForm);
            }
        } else {
            $id = $this->_getParam('iduser', 0);
            if ($id > 0) {
                $user = new Application_Model_Auth_Uzytkownicy();
                //$student = new BazaZF_Student();
                $this->form_admin_edyt_user->populate($user->getUser($id));
            }
        }
    }

    function adminpoedycjikontaAction() {
        $this->view->title = "Dane po edycji";

        $k = $this->_getParam('idedyt');
        $dane = $this->view->user->getUser($k);
        $this->view->iduser = $dane['iduser'];
        $this->view->login = $dane['login'];
        $this->view->plec = $dane['gender'];
        $this->view->email = $dane['email'];
        $this->view->rola = $dane['idacctype'];

        $db = Zend_Db_Table::getDefaultAdapter();
        $query = $db->select()
                ->from('acctype', 'type')
                ->where('idacctype = ?', $this->view->rola);
        $result = $db->fetchRow($query);

        foreach ($result as $k) {
            $this->view->rola = $k;
        }
    }

    public function usunuseraAction() {
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Tak') {
                $id = $this->getRequest()->getPost('id');
                $user = new Application_Model_Auth_Uzytkownicy();
                //$student = new BazaZF_Student();
                $user->deleteUser($id);
            }

            $this->_helper->redirector('adminuzytkownicy');
        } else {
            $id = $this->_getParam('iduser', 0);
            $user = new Application_Model_Auth_Uzytkownicy();
            //$student = new BazaZF_Student();
            $this->view->user = $user->getUser($id);
        }
    }

    public function adminfilmyAction() {
        $this->view->title = "Lista Obiektow";
        $this->view->films = $this->view->films->fetchAll();
    }

    public function dodajfilmAction() {
        $this->view->title = "Dodaj Obiekt";
        $this->view->films = $this->view->films->fetchAll();


        if ($this->getRequest()->isPost()) {
            self::$daneForm = $this->_request->getPost();
            if ($this->view->form_dodaj_film->isValid(self::$daneForm)) {
                $tytul = $this->view->form_dodaj_film->getValue('title');
                $opis = $this->view->form_dodaj_film->getValue('description');
                $gatunek = $this->view->form_dodaj_film->getValue('idgenre');
                $plik = $this->view->form_dodaj_film->getValue('miniature');
                $link = $this->view->form_dodaj_film->getValue('source');
                $typ = $this->view->form_dodaj_film->getValue('idmovietype');
                $film = new Application_Model_Auth_Filmy();
                //$student = new BazaZF_Student();
               

                $db = Zend_Db_Table::getDefaultAdapter();
               
                //sprawdzenie czy istnieje juz taki link w bazie
                $querylink = $db->select()
                        ->from('movie', 'source')
                        ->where('source = ?', $link);

                $resultlink = $db->fetchRow($querylink);
                
                //echo $link ." ".$resultlink['source'];

                if ($resultlink) {
                    echo "<p><h3>Obiekt o podanym linku już istnieje, podaj inny link!</h3></p>";
                    return;
                }


                $querygatunek = $db->select()
                        ->from('genres', 'genre')
                        ->where('genre = ?', $gatunek);

                $resultgatunek = $db->fetchRow($querygatunek);
                
                $film->addFilm($tytul, $plik, $opis, $link, $gatunek, $typ);
                
                  
                $this->view->tytul = $tytul;
                $this->view->description = $opis;
                $this->view->genre = $resultgatunek['genre'];
                $this->view->miniature = $plik;
                $this->view->source = $link;

               $this->_helper->redirector('adminfilmy');
            } else {
                $this->view->form_dodaj_film->populate(self::$daneForm);
            }
        }
    }
    
    
    public function edytujfilmAction() {
        $this->view->title = "Edytuj dane";


        if ($this->getRequest()->isPost()) {
            self::$daneForm = $this->_request->getPost();
            if ($this->view->form_dodaj_film->isValid(self::$daneForm)) {
                $tytul = $this->view->form_dodaj_film->getValue('title');
                $opis = $this->view->form_dodaj_film->getValue('description');
                $gatunek = $this->view->form_dodaj_film->getValue('idgenre');
                $plik = $this->view->form_dodaj_film->getValue('miniature');
                $link = $this->view->form_dodaj_film->getValue('source');
                $typ = $this->view->form_dodaj_film->getValue('idmovietype');
                $film = new Application_Model_Auth_Filmy();

                $db = Zend_Db_Table::getDefaultAdapter();

                ///sprawdzenie czy istnieje juz taki link w bazie
                $querylink = $db->select()
                        ->from('movie', 'source')
                        ->where('source = ?', $link);

                $resultlink = $db->fetchRow($querylink);
                
                //echo $link ." ".$resultlink['source'];
                
                 //zwrocenie id filmu o podanym linku z bazy jesli istnieje
                $queryid = $db->select()
                        ->from('movie', 'idmovie')
                        ->where('source = ?', $link);

                $resultid = $db->fetchRow($queryid);

                if ($resultlink && ($resultid['idmovie'] != $this->_request->getParam('idmovie'))) {
                    echo "<p><h3>Obiekt o podanym linku już istnieje, podaj inny link!</h3></p>";
                    return;
                }


               
                $user = new Application_Model_Auth_Filmy();
                $user->editFilm($this->_request->getParam('idmovie'), $tytul, $plik, $opis,
                                $link, $gatunek, $typ);
                


                $idedyt = $this->_request->getParam('idmovie');

                
                $this->_redirect('/zalogowani/poedycjifilmu?idedyt=' . $idedyt);
            } else {
                $this->form_dodaj_film->populate(self::$daneForm);
            }
        } else {
            $id = $this->_getParam('idmovie', 0);
            if ($id > 0) {
                $film = new Application_Model_Auth_Filmy();
                //$student = new BazaZF_Student();
                $this->form_dodaj_film->populate($film->getFilm($id));
            }
        }
    }

    public function poedycjifilmuAction() {
        $this->view->title = "Dane po edycji";

        $k = $this->_getParam('idedyt');
        $dane = $this->view->films->getFilm($k);
        $this->view->id = $dane['idmovie'];
        $this->view->tytul = $dane['title'];
        $this->view->miniature = $dane['miniature'];
        $this->view->description = $dane['description'];
        $this->view->genre = $dane['idgenre'];
        $this->view->movietype = $dane['idmovietype'];
        $this->view->source = $dane['source'];
        
       
        $db = Zend_Db_Table::getDefaultAdapter();
        $querygatunek = $db->select()
                        ->from('genres', 'genre')
                        ->where('idgenre = ?', $this->view->genre);

        $resultgatunek = $db->fetchRow($querygatunek);
                
        $querytyp = $db->select()
                        ->from('movietype', 'movietype')
                        ->where('idmovietype = ?', $this->view->movietype);

        $resulttyp = $db->fetchRow($querytyp);

        $this->view->genre = $resultgatunek['genre'];
        $this->view->movietype = $resulttyp['movietype'];
        
        
    }
        
    public function usunfilmAction() {
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Tak') {
                $id = $this->getRequest()->getPost('id');
                $film = new Application_Model_Auth_Filmy();
                //$student = new BazaZF_Student();
                $film->deleteFilm($id);
            }

            $this->_helper->redirector('adminfilmy');
        } else {
            $id = $this->_getParam('idmovie', 0);
            $film = new Application_Model_Auth_Filmy();
            //$student = new BazaZF_Student();
            $this->view->films = $film->getFilm($id);
        }
    }
    
    

public function wyszukiwanieAction(){     
      if ($this->_request->isPost()) {
             self::$daneForm = $this->_request->getPost();
             if ($this->form_szukaj->isValid(self::$daneForm)){
                 $this->szukaj = $this->form_szukaj->getValue('szukaj');
                 $db = Zend_Db_Table::getDefaultAdapter();
                 $select = $db->select();
                 $select->from('movie');
                 $tekst = $this->szukaj;
                 function zamiana_znakow($tekst){
                        $tabelaznakow = Array(
                            "ą" => "a", "Ą" => "A", "ć" => "c", "Ć" => "C",
                            "ę" => "e", "Ę" => "E", "ł" => "l", "Ł" => "L",
                            "ó" => "o", "Ó" => "O", "ś" => "s", "Ś" => "S",
                            "ż" => "z", "Ż" => "Z", "ź" => "z", "Ź" => "Z",
                            "ń" => "n", "Ń" => "N",);
                        return strtr($tekst,$tabelaznakow);
                 }
                 $zmieniony = zamiana_znakow($tekst);
                 if($zmieniony == null){$zmieniony="Zabezpieczenie przed brakiem frazy";}
                 $select->where('title LIKE ?',"%".$zmieniony."%");
                 $wynik = $db->fetchAll($select);
                 
                 $this->view->title = "Wyniki wyszukiwania";
                 $this->view->wynik = $wynik;
                 $this->view->wynik = $wynik;
             }else {
                 $this->form->populate(self::$daneForm);
             }
      } 
      $this->view->form = $this->form;
}
}